package me.zhengjie.poemmanager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.poemmanager.domain.ObjectLike;
import me.zhengjie.poemmanager.mapper.ObjectLikeMapper;
import me.zhengjie.poemmanager.service.CommentLikeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentLikeServiceImpl extends ServiceImpl<ObjectLikeMapper, ObjectLike> implements CommentLikeService {
    private final ObjectLikeMapper objectLikeMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public ObjectLike create(ObjectLike resources) {
        switch (resources.getCommentType()) {
            case 0:
                objectLikeMapper.createPostCommentLike(resources);
                objectLikeMapper.addPostCommentLikeCount(resources.getObjectId());
                break;
            case 1:
                objectLikeMapper.createPoemUserCommentLike(resources);
                objectLikeMapper.addPoemUserCommentLikeCount(resources.getObjectId());
                break;
            case 2:
                objectLikeMapper.createPoemPoetCommentLike(resources);
                objectLikeMapper.addPoemPoetCommentLikeCount(resources.getObjectId());
                break;
            case 3:
                objectLikeMapper.createRecitationCommentLike(resources);
                objectLikeMapper.addRecitationCommentLikeCount(resources.getObjectId());
                break;
            default:
                throw new IllegalArgumentException("无效的评论点赞类型: " + resources.getObjectType());
        }
        return resources; //已采用实体回填
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(Integer commentType, List<Integer> ids) {
        for(Integer id : ids) {
            switch (commentType) {
                case 0 :
                    objectLikeMapper.subPostCommentLikeCount((objectLikeMapper.selectByPostCommentLikeId(id)).getObjectId());
                    objectLikeMapper.deletePostCommentLike(id);
                    break;
                case 1:
                    objectLikeMapper.subPoemUserCommentLikeCount((objectLikeMapper.selectByPoemUserCommentLikeId(id)).getObjectId());
                    objectLikeMapper.deletePoemUserCommentLike(id);
                    break;
                case 2:
                    objectLikeMapper.subPoemPoetCommentLikeCount((objectLikeMapper.selectByPoemPoetCommentLikeId(id)).getObjectId());
                    objectLikeMapper.deletePoemPoetCommentLike(id);
                    break;
                case 3:
                    objectLikeMapper.subRecitationCommentLikeCount((objectLikeMapper.selectByRecitationCommentLikeId(id)).getObjectId());
                    objectLikeMapper.deleteRecitationCommentLike(id);
                    break;
                default:
                    throw new IllegalArgumentException("无效的评论点赞类型: " + commentType);
            }
        }
    }

    @Override
    public ObjectLike isLike(Integer commentType, ObjectLike like) {
        switch (commentType) {
            case 0 :
                return objectLikeMapper.isPostCommentLike(like.getObjectId(),like.getUserName());
            case 1:
                return objectLikeMapper.isPoemUserCommentLike(like.getObjectId(),like.getUserName());
            case 2:
                return objectLikeMapper.isPoemPoetCommentLike(like.getObjectId(),like.getUserName());
            case 3:
                return objectLikeMapper.isRecitationCommentLike(like.getObjectId(),like.getUserName());
            default:
                throw new IllegalArgumentException("无效的评论点赞类型: " + commentType);
        }
    }


}
